package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.*;
import cn.wolfcode.wms.exception.LoginException;
import cn.wolfcode.wms.mapper.ProductStockMapper;
import cn.wolfcode.wms.mapper.SaleAccountMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.ProductStockQueryObject;
import cn.wolfcode.wms.service.ProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
@Service
public class ProductStockServiceImpl implements ProductStockService{
    @Autowired
    private ProductStockMapper productStockMapper;
    @Autowired
    private SaleAccountMapper saleAccountMapper;


    public PageResult selectAll(ProductStockQueryObject qo) {
        Integer totalcount = productStockMapper.queryForCount(qo);

        List<ProductStock> data = productStockMapper.selectAll(qo);
        return new PageResult(totalcount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    public void stockIncome(StockIncomeBill bill) {
        for (StockIncomeBillItem item : bill.getItems()) {
            ProductStock ps = productStockMapper.selectByproductIdAndDepotId(item.getProduct().getId(), bill.getDepot().getId());
            if (ps==null){
                ps=new ProductStock();
                ps.setProduct(item.getProduct());
                ps.setDepot(bill.getDepot());
                ps.setPrice(item.getCostPrice());
                ps.setStoreNumber(item.getNumber());
                ps.setAmount(item.getAmount());
                productStockMapper.insert(ps);
            }else {
                ps.setStoreNumber(ps.getStoreNumber().add(item.getNumber()));
                ps.setAmount(ps.getAmount().add(item.getAmount()));
                ps.setPrice(ps.getAmount().divide(ps.getStoreNumber(),2, RoundingMode.HALF_UP));
                productStockMapper.updateByPrimaryKey(ps);
            }
        }
    }

    public void stockOutcome(StockOutcomeBill bill) {
        for (StockOutcomeBillItem item : bill.getItems()) {
            ProductStock ps = productStockMapper.selectByproductIdAndDepotId(item.getProduct().getId(), bill.getDepot().getId());
            if (ps==null){
                throw  new LoginException("在"+bill.getDepot().getName()+"仓库中没有"+item.getProduct().getName()+"货品");
            }
            //有库存单是出货量大于她的库存量
            if(ps.getStoreNumber().compareTo(item.getNumber())<0){
                throw  new LoginException("在"+bill.getDepot().getName()+"货品在中"+item.getProduct().getName()+"仓库中数量不足");

            }
            //库存量大于或等于的情况
            ps.setStoreNumber(ps.getStoreNumber().subtract(item.getNumber()));
            ps.setAmount(ps.getPrice().multiply(ps.getStoreNumber()).setScale(2, RoundingMode.HALF_UP));
            productStockMapper.updateByPrimaryKey(ps);
            //记录销售额
            SaleAccount sa = new SaleAccount();
            sa.setClientId(bill.getClient().getId());
            sa.setCostPrice(ps.getPrice());
            sa.setNumber(item.getNumber());
            sa.setSalePrice(item.getSalePrice());
            sa.setSaleAmount(item.getAmount());
            sa.setCostAmount(sa.getNumber().multiply(sa.getCostPrice()).setScale(2, RoundingMode.HALF_UP));
            sa.setVdate(bill.getVdate());
            saleAccountMapper.insert(sa);
        }
    }
}
